package com.hhxy.web;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * 发送Cookie
 */
@WebServlet("/AServlet")
public class AServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //0、将要存储的中文进行URL编码
        String name = "张三";
        String encode = URLEncoder.encode(name, "UTF-8");
        //测试：
        System.out.println(encode);//输出：%E5%BC%A0%E4%B8%89
        //1、创建Coolie对象，并设置值
        Cookie cookie = new Cookie("username",encode);
        //2、发送Coolie对象
        response.addCookie(cookie);
    }
}